package com.detech.sbom.base.log.serviceimpl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.detech.sbom.base.config.mybatis.PageParam;
import com.detech.sbom.base.log.service.LogService;
import com.detech.sbom.base.mybatis.log.dao.TbLogP6spyDao;
import com.detech.sbom.base.mybatis.log.dao.TbLogSysDao;
import com.detech.sbom.base.mybatis.log.entity.TbLogP6spyEntity;
import com.detech.sbom.base.mybatis.log.entity.TbLogSysEntity;
import com.detech.sbom.base.util.UserFun;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Transactional(rollbackFor = Exception.class)
public class LogServiceImpl implements LogService {

    @Value(value = "${spring.application.name}")
    private String xmmc;
    @Resource
    private TbLogSysDao tbLogSysDao;
    @Resource
    private TbLogP6spyDao tbLogP6spyDao;

    @Override
    public void saveSysLog(TbLogSysEntity sysLogEntity){
        tbLogSysDao.insert(sysLogEntity);
    }

    @Override
    public void writeDB(String message) {
        String[] msgSz = null;
        /** 检查信息合法性 **/
        boolean flag = false;
        if(message!=null && message.trim().length()>0){
            msgSz = message.split("&");
            //信息拆分后，长度为3
            if(msgSz.length==3){
                flag = true;
            }
        }
        //性能监控信息格式不符
        if(flag==false){
            return;
        }
        int xhsj = new Integer(msgSz[0]);//消耗时间
        String ybySql = msgSz[1];//预编译SQL
        String paramSql = msgSz[2];//带参数SQL
        TbLogP6spyEntity entity = new TbLogP6spyEntity();
        entity.setId(UserFun.getUUID32());
        entity.setLogTime(new Date());
        entity.setXmmc(xmmc);
        entity.setParamSql(paramSql);
        entity.setYbySql(ybySql);
        entity.setXhsj(xhsj);
        tbLogP6spyDao.insert(entity);
    }

    @Override
    public Page<TbLogSysEntity> loadSysLogList(PageParam pageParam, String content, String logType, String module, String operator, String ip, String logTime)  {
        LambdaQueryWrapper<TbLogSysEntity> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        userLambdaQueryWrapper.like(content!=null && content.trim().length()>0,TbLogSysEntity::getContent , content);
        userLambdaQueryWrapper.eq(logType!=null && logType.trim().length()>0,TbLogSysEntity::getLogType , logType);
        userLambdaQueryWrapper.eq(module!=null && module.trim().length()>0,TbLogSysEntity::getModule , module);
        userLambdaQueryWrapper.like(operator!=null && operator.trim().length()>0,TbLogSysEntity::getOperator , operator);
        userLambdaQueryWrapper.eq(ip!=null && ip.trim().length()>0,TbLogSysEntity::getIp , ip);
        if(logTime!=null&&logTime.trim().length()>0){
            Date logTimeDate = DateUtil.parse(logTime,"yyyy-MM-dd").toJdkDate();
            Date logtimeAddOne = DateUtil.offsetDay(logTimeDate,1).toJdkDate();
            userLambdaQueryWrapper.between(TbLogSysEntity::getLogTime ,logTimeDate,logtimeAddOne);
        }
        userLambdaQueryWrapper.orderByDesc(TbLogSysEntity::getLogTime);
        return tbLogSysDao.selectPage(pageParam.build() , userLambdaQueryWrapper);
    }

    @Override
    public Page<TbLogP6spyEntity> loadP6spyLogList(PageParam pageParam, String ybySql,String paramSql,Integer xhsj,String xmmc, String logTime)  {
        LambdaQueryWrapper<TbLogP6spyEntity> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        userLambdaQueryWrapper.like(ybySql!=null && ybySql.trim().length()>0,TbLogP6spyEntity::getYbySql , ybySql);
        userLambdaQueryWrapper.like(paramSql!=null && paramSql.trim().length()>0,TbLogP6spyEntity::getParamSql , paramSql);
        userLambdaQueryWrapper.ge(xhsj!=null,TbLogP6spyEntity::getXhsj , xhsj);
        userLambdaQueryWrapper.eq(xmmc!=null && xmmc.trim().length()>0,TbLogP6spyEntity::getXmmc , xmmc);
        if(logTime!=null&&logTime.trim().length()>0){
            Date logTimeDate = DateUtil.parse(logTime,"yyyy-MM-dd").toJdkDate();
            Date logtimeAddOne = DateUtil.offsetDay(logTimeDate,1).toJdkDate();
            userLambdaQueryWrapper.between(TbLogP6spyEntity::getLogTime ,logTimeDate,logtimeAddOne);
        }
        userLambdaQueryWrapper.orderByDesc(TbLogP6spyEntity::getLogTime);
        return tbLogP6spyDao.selectPage(pageParam.build() , userLambdaQueryWrapper);
    }

    @Override
    public TbLogSysEntity loadSysLogDetail(String id) {
        return tbLogSysDao.selectById(id);
    }

    @Override
    public TbLogP6spyEntity loadP6spyLogDetail(String id) {
        return tbLogP6spyDao.selectById(id);
    }

}
